import axios from "axios";
import { IUser, IFav } from "../types";
import * as request from './request'
interface BaseRes {
  stat: string;
  message: string;
}

interface LoginRes extends BaseRes {
  token: string;
  user: IUser;
}

interface UserInfo extends BaseRes {
  data: IUser;
}

interface listInfo extends BaseRes {
  items: IFav[];
}

 interface fileRes extends BaseRes {
  data:Buffer
} 

interface isInterested extends BaseRes {
  data: boolean;
}

// 我的页面调用的pai
// 登陆注册登出调用的api
/**
 * 用户登录
 * @param username 用户名
 * @param password 密码
 * @returns
 */
export async function userlogin(username?: string, password?: string) {
  const result = await axios.post<LoginRes>("/api/user/front/loginPlus", {
    username,
    password,
  });
  return result.data;
}

//注销登录
export async function logout() {
  const result = await axios.post<BaseRes>("/api/user/logout");
  return result.data;
}

//获取用户信息
export async function userInfo() {
  const result = await axios.post<UserInfo>("/api/user/front/getInfo");
  return result.data;
}

//收藏页列表
export async function listFav() {
  const result = await axios.post<listInfo>("/api/user/fav/list");
  return result.data;
}

// 查看是否收藏
export async function findFav(id: string) {
  const result = await axios.post<isInterested>("/api/user/fav/find", { id });
  return result.data;
}

// 添加收藏
export async function addFav(id: string) {
  const result = await axios.post<listInfo>("/api/user/fav/add", {
    IRecruitId: id,
  });
  return result.data;
}

//删除收藏
export async function removeFav(id: string) {
  const result = await axios.post<BaseRes>("/api/user/fav/remove", { id });
  return result.data;
}

//检验密码
export async function test(password?: string) {
  const result = await axios.post<BaseRes>("/api/user/front/test", {
    password,
  });
  return result.data;
}

//修改密码
export async function addInfo(password?: string) {
  const result = await axios.post<BaseRes>("/api/user/front/addInfo", {
    password,
  });
  return result.data;
}

//修改个人信息
export async function testInfo(
  username?: string,
  positionName?: string,
  sex?: number,
  avatar?: string
) {
  const result = await axios.post<BaseRes>("/api/user/front/addInfo", {
    username,
    positionName,
    sex,
    avatar,
  });
  return result.data;
}

export async function upload(file: File) {
  let data = new FormData()
  data.append('file', file)
  const res = await request.post<fileRes>("/api/user/upload",data);
  return res.data;
}


export async function userAvatar(file: File) {
  console.log(file)
  const data = new FormData()
  data.append('file', file)
  const result = await axios.post('/api/user/upload', data)
  return result.data;
}